package com.dh.project.controller;

import com.dh.project.model.EngineerPhone;
import com.dh.project.model.PageInfo;
import com.dh.project.model.PageModel;
import com.dh.project.other.ModelRes;
import com.dh.project.service.EngineerPhoneService;
import com.dh.project.service.ServiceProjectService;
import com.dh.project.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by admin on 2017/4/13.
 */
@Controller
public class LogisticsController {

    private EngineerPhoneService engineerPhoneService;

    private ServiceProjectService serviceProjectService;

    @Autowired
    private LogisticsController(EngineerPhoneService engineerPhoneService,ServiceProjectService serviceProjectService){
        this.engineerPhoneService = engineerPhoneService;
        this.serviceProjectService = serviceProjectService;
    }


    /**
     * 工程师电话列表
     * @param model
     * @return
     */
    @RequestMapping(value = "selectEngineerPhoneList",produces = "text/html;charset=utf-8")
    public String selectEngineerPhoneList(EngineerPhone engineerPhone,int page,Model model){
        Map map = new HashMap();
        if (engineerPhone.getEngineer_id() != null){
            map.put("engineer_id",engineerPhone.getEngineer_id());
        }
        if (engineerPhone.getEngineer_name() != null){
            map.put("engineer_name",engineerPhone.getEngineer_name());
        }
        map.put("page_start",(page-1)*10);
        map.put("page_end",10);
        List<EngineerPhone> engineerPhoneList = engineerPhoneService.selectEngineerPhoneListByMap(map);

        PageModel pageModel = engineerPhoneService.selectEngineerPhoneListByMapCount(map);
        PageInfo pageInfo = PageUtil.getPageInfo(pageModel,page,10);
        pageInfo.setPage_href(PageUtil.getPageHref(engineerPhone,4));
        pageInfo.setPage_index(page);
        pageInfo.setPage_count(PageUtil.getPageCount(pageModel.getPage_index(),10));

        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("engineerPhone",engineerPhone);
        model.addAttribute("engineerPhoneList",engineerPhoneList);
        return "engineer_list";
    }

    /**
     * 添加工程师电话
     * @param engineerPhone
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "insertEngineerPhone",produces = "text/html;charset=utf-8")
    public Object insertEngineerPhone(EngineerPhone engineerPhone){
        Map map = new HashMap();
        map.put("engineer_phone",engineerPhone.getEngineer_phone());
        List<EngineerPhone> engineerPhones = engineerPhoneService.selectByMap(map);
        if (engineerPhones.size()>0){
            return new ModelRes(ModelRes.Status.ERROR,"该手机号码已存在!");
        }

        boolean isSuccess = engineerPhoneService.insert(engineerPhone);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"添加失败!");
        }
    }

    /**
     * 修改工程师
     * @param engineerPhone
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateEngineerPhone",produces = "text/html;charset=utf-8")
    public Object updateEngineerPhone(EngineerPhone engineerPhone){
        Map map = new HashMap();
        map.put("engineer_phone",engineerPhone.getEngineer_phone());
        List<EngineerPhone> engineerPhones = engineerPhoneService.selectByMap(map);
        if (engineerPhones.size() != 0){
            if (engineerPhones.get(0).getEngineer_id() != engineerPhone.getEngineer_id()){
                return new ModelRes(ModelRes.Status.ERROR,"该手机号码已存在，修改失败!");
            }
        }

        EngineerPhone engineerPhone1 = engineerPhoneService.selectById(engineerPhone.getEngineer_id());
        if (engineerPhone.getEngineer_name() != null){
            engineerPhone1.setEngineer_name(engineerPhone.getEngineer_name());
        }
        if (engineerPhone.getEngineer_phone() != null){
            engineerPhone1.setEngineer_phone(engineerPhone.getEngineer_phone());
        }
        if (engineerPhone.getPhone_address() != null){
            engineerPhone1.setPhone_address(engineerPhone.getPhone_address());
        }
        boolean isSuccess = engineerPhoneService.updateById(engineerPhone1);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"修改失败!");
        }
    }

    /**
     * 删除工程师
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deleteEngineer",produces = "text/html;charset=utf-8")
    public Object deleteEngineer(int admin_id){
        boolean isSuccess = engineerPhoneService.deleteById(admin_id);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"删除失败!");
        }
    }


}
